import { Injectable } from "@angular/core";
import { AppSettings } from "../../shared/config/app-settings";
import { HttpClient, HttpParams } from "@angular/common/http";
import { map } from "rxjs/operators";
import * as _ from 'lodash';
import { Observable } from "rxjs";
import { ParamMap } from '@angular/router';
@Injectable({
    providedIn: 'root'
})
export class PropertyService {

    constructor(private httpClient: HttpClient) { }

    // getAllProperties() {
    //     let header = _.cloneDeep(AppSettings.httpServerHeader);
    //     return this.httpClient.get<Array<any>>(`${AppSettings.server_base}/property`, header).pipe(
    //         map((res) => res['data'])
    //     );
    // }

    getAllProperties(filter?): Observable<any> {
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        if (filter) {
          header.params = filter;
        }
        return this.httpClient.get<Array<any>>(`${AppSettings.server_base}/property`, header).pipe(map(res => res['success'] ? res['data'] : []));
      }

    getPropertyById(id) {
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.get<any>(`${AppSettings.server_base}/property/${id}`, header).pipe(map((item) => {
            return item.data;
        }));
    }

    delete(ids:any[]):Observable<any>{
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        header.params = new HttpParams();
        header.params = header.params.set('id', ids.join(','));
        return this.httpClient.delete<any>(`${AppSettings.server_base}/property`, header).pipe(map((item) => {
            return item.data;
        }));
    }

    

    updatePropertyByKeyValues(id, KVs){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.patch<any>(`${AppSettings.server_base}/property/${id}`,KVs, header);
    }

    createSaleNotification(note){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.post<any>(`${AppSettings.server_base}/saleNotification`,note, header);
    }

    updateSaleNotification(note){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.put<any>(`${AppSettings.server_base}/saleNotification`,note, header);
    }

    createSolicitor(solicitor){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.post<any>(`${AppSettings.server_base}/solicitor`, solicitor, header)
            .pipe(
                map(res => res.data),
            );
    }

    notifySale(id, KVs){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.put<any>(`${AppSettings.server_base}/saleNotification/${id}`,KVs, header);
    }

    getCustomerInfo(id) {
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.get<any>(`${AppSettings.server_base}/saleNotification/${id}`, header)
            .pipe(
                map(item => item.data),
            );
    }

    updateProperty(property) {
        let header = _.cloneDeep(AppSettings.httpServerHeader);
        return this.httpClient.post<any>(`${AppSettings.server_base}/property`, property, header);
    }

    updateSalesStatus(props){
        let header = _.cloneDeep(AppSettings.httpServerHeader);
      
        return this.httpClient.put<any>(`${AppSettings.server_base}/property/status-update`,props, header);
    }

    uploadFile(attachment): Observable<any> {
        const formData: FormData = new FormData();
        let files = attachment.files;
        for (let i = 0; i < files.length; i++) {
            formData.append('file', files[i], files[i].name);
        }
        formData.append("belongToCategory", attachment.belongToCategory);
        formData.append("belongToID", attachment.belongToID);
        return this.httpClient.post(`${AppSettings.server_base}/common/attachment`, formData);
    }


    //utils
    getMainImages(property){
        return property['property_main_image'] || [];
    }

    getMainImageURL(property):string{
        let url = "";
        if(_.isEmpty(this.getMainImages(property))){
            url = "/assets/img/defaulHouseMainImage.jpg";
        }else{
            url = this.getMainImages(property)[0].filePath;
        }
        return url;

    }

    escapeText(text, isEscape) {
        return isEscape ? _.escape(text) : _.unescape(text);
    }

    escapeContent(property, field, isEscape) {
        if (isEscape)
            property[field] = _.escape(property[field])
        else
            property[field] = _.unescape(property[field]);
        return property[field];
    }

    escapeAll(property, isEscape) {
        if (isEscape) {
            property.textContractOfSale = _.escape(property['textContractOfSale']);
            property.textDepositForm = _.escape(property['textDepositForm']);
            property.textSolicitor = _.escape(property['textSolicitor']);
            property.textBillsCharges = _.escape(property['textBillsCharges']);
            property.textManagementAgreement = _.escape(property['textManagementAgreement']);
            property.textOthers = _.escape(property['textOthers']);
        }
        else {
            property.textContractOfSale = _.unescape(property['textContractOfSale']);
            property.textDepositForm = _.unescape(property['textDepositForm']);
            property.textSolicitor = _.unescape(property['textSolicitor']);
            property.textBillsCharges = _.unescape(property['textBillsCharges']);
            property.textManagementAgreement = _.unescape(property['textManagementAgreement']);
            property.textOthers = _.unescape(property['textOthers']);
        }
    }



}